const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gridItem: [{
      icon: '/images/icon-protein-detection.png',
      class: 'icon-protein-detection',
      text: '蛋白检测',
      url: '../protein-calc/protein-calc'
    },
    {
      icon: '/images/icon-collect.png',
      class: 'icon-collect',
      text: '课程收藏',
      url: `../favorites/favorites?type=2`
    },
    // {
    //     icon: '/images/icon-course-download.png',
    //     class: 'icon-course-download',
    //     text: '课程缓存',
    //     url: ''
    // },
    {
      icon: '/images/icon-watch-history.png',
      class: 'icon-watch-history',
      text: '观看历史',
      url: '../watch-history/watch-history'
    },
    // {
    //   icon: '/images/icon-comment.png',
    //   class: 'icon-comment',
    //   text: '我的评论',
    //   url: ''
    // },
    {
      icon: '/images/icon-change-password.png',
      class: 'icon-change-password',
      text: '修改密码',
      url: '../change-password/change-password'
    },
    {
      icon: '/images/icon-logout.png',
      class: 'icon-logout',
      text: '退出登录'
    }
    ],
    userInfo: {},
    showMask: false,
    showPopup: false,
    num: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    })
    this.getlearnedCourse();
  },
  onShow() {
    this.setData({
      showMask: false,
      showPopup: false,
    })
  },
  getlearnedCourse() {
    app.fetchRequest({
      url: `/user/exam/query/1`,
      data: {
      },
      success: (res) => {
        this.setData({
          num: res.total
        })
      }
    })
  },
  navigateTo(e) {
    const index = e.currentTarget.dataset.index;
    switch (index) {
      case 4:
        this.setData({
          showMask: true,
          showPopup: true
        })
        break;
      default:
        if (this.data.gridItem[index].url) {
          wx.navigateTo({
            url: this.data.gridItem[index].url,
          })
        }
        break
    }
  },
  toCourse() {
    wx.navigateTo({
      url: '../favorites/favorites?type=1',
    })
  },
  confirm() {
    wx.removeStorageSync('token');
    wx.removeStorageSync('userInfo');
    wx.navigateTo({
      url: '../login/login',
    })
  },
  hiddenMask() {
    this.setData({
      showMask: false,
      showPopup: false
    })
  }
})